package com.judysen.jsqlweb.configuration;

import com.judysen.jsqlweb.filters.ThreadContext;
import com.judysen.jsqlweb.utils.JsqlDynamicDatasource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfiguration {
    @Value("${jsql.database.url}")
    private String databaseUrl;
    @Value("${jsql.database.userName}")
    private String userName;
    @Value("${jsql.database.password}")
    private String password;

    @Autowired
    private ThreadContext threadContext;

    @Bean
    public JsqlDynamicDatasource myDynamicDatasource(){
        return new JsqlDynamicDatasource(databaseUrl,userName,password);
    }
    @Bean
    public FilterRegistrationBean filters(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(threadContext);
        registration.addUrlPatterns("/*");
        registration.setName("threadContext");
        registration.setOrder(-1);  //值越小，Filter越靠前。
        return registration;
    }
}
